package com.student.web.student;

import com.google.gson.Gson;
import com.student.dao.impl.StudentDaoImpl;
import com.student.pojo.Major;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 根据学院获得相对应的专业
 * 需要传入参数:
 * 		request:
 * 			collegeId(学院ID)
 * @author lin945
 *
 */
@WebServlet("/getMajor")
public class GetMajorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private StudentDaoImpl studentDao = new StudentDaoImpl();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		HttpSession session = request.getSession();
		if (session.getAttribute("username") == null) {
			response.sendRedirect(request.getContextPath() + "/login");
			return;
		}
		Integer collegeId = Integer.parseInt(request.getParameter("collegeId"));

		List<Major> list = studentDao.queryMajorByCollege(collegeId);
		response.setHeader("Content-Type", "application/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String major = gson.toJson(list);
		out.print(major);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
